﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using HandyControl.Tools.Extension;
using SSHTool.Core.Message;
using System;
using System.Windows;

namespace SSHTool.ViewModel
{
    public class EditFileViewModel : ObservableObject, IDialogResultable<int>
    {
        public EditFileViewModel() { }

        private string title;

        public string Title { get => title; set => SetProperty(ref title, value); }

        private string richText;
        public string RichText { get => richText; set => SetProperty(ref richText, value); }

        private string path;

        public string Path { get => path; set => SetProperty(ref path, value); }

        private int result;
        public int Result { get => result; set => SetProperty(ref result, value); }

        private bool readOnly;

        public bool ReadOnly
        {
            get => readOnly;
            set
            {
                SetProperty(ref readOnly, value);

                ShowBtn = readOnly ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        private Visibility showBtn;

        public Visibility ShowBtn { get => showBtn; private set => SetProperty(ref showBtn, value); }

        public Action CloseAction { get; set; }

        public RelayCommand SureCmd => new RelayCommand(() =>
        {
            Result = 1;
            CloseAction?.Invoke();
        });

        public RelayCommand CancelCmd => new RelayCommand(() =>
        {
            Result = 0;
            CloseAction?.Invoke();
        });

        public RelayCommand SaveCmd => new RelayCommand(() =>
        {
            WeakReferenceMessenger.Default.Send(new SaveFileContentMessage(Path, RichText));
        });
    }
}
